﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Category_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("CatId").ToString.Equals("0") Then
                getParent()
                hidID.Value = Request.QueryString("CatId")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub getParent()
        ddlParent.Items.Clear()
        Dim data As New VSEntities
        Dim p As List(Of Category) = (From q In data.Categories Select q).ToList
        ddlParent.DataValueField = "CatId"
        ddlParent.DataTextField = "Title"
        RecursiveFillTree(p, 0)
        ddlParent.Items.Insert(0, New ListItem("---Chọn Category cấp cha---", "0"))
    End Sub
    Private Sub RecursiveFillTree(ByVal dtParent As List(Of Category), ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Dim data As New VSEntities
        Dim dv As List(Of Category) = (From q In data.Categories Where q.ParentId = parentID Select q).ToList
        Dim i As Integer

        If dv.Count > 0 Then
            For i = 0 To dv.Count - 1
                ddlParent.Items.Add(New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).CatId.ToString()))
                RecursiveFillTree(dtParent, Integer.Parse(dv.Item(i).CatId.ToString()))
            Next
        End If

        level -= 1
        'on the each function end level will decrement by 1
    End Sub

    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim p As Category = (From q In Data.Categories Where q.CatId = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
            txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
            ddlParent.SelectedValue = IIf(IsNothing(p.ParentId) = True, 0, p.ParentId)
            txtTitleEn.Text = IIf(IsNothing(p.TitleEn) = True, "", p.TitleEn)
            If Not p.MainCate Is Nothing And p.MainCate = True Then
                chkMainCate.Checked = True
            Else
                chkMainCate.Checked = False

            End If

        End If
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Category = (From q In data.Categories Where q.CatId = hidID.Value).SingleOrDefault
            Try
                p.CatId = hidID.Value
                p.Title = txtTitle.Text.Trim()
                p.Description = txtDescription.Text
                p.QueryString = makeURLFriendly(txtTitle.Text.Trim())
                p.ParentId = ddlParent.SelectedValue
                If (p.ParentId <> ddlParent.SelectedValue) Then
                    Dim parent As Integer = CInt(ddlParent.SelectedValue)
                    Dim t = (From h In data.Menus Where h.ParentId = parent Order By h.Sort Descending Select h).FirstOrDefault
                    If Not t Is Nothing Then
                        p.Sort = CInt(t.Sort.ToString) + 1
                    Else
                        p.Sort = 1
                    End If
                End If
                p.Modified = Now
                p.Modifier = Session("UserName")
                p.MainCate = chkMainCate.Checked
                p.TitleEn = txtTitleEn.Text
                data.SaveChanges()
                    Insert_App_Log("Update Category:" & txtTitle.Text.Trim & "", Function_Name.Category, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Category/List.aspx?CatId=" & ddlParent.SelectedValue.ToString & "';", Me.Page, True)

            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using

       
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

End Class
